﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UTC_Project_Manager.Model.Infrastructure;
using System.ComponentModel;

namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for ReminderTaskControl.xaml
    /// </summary>
    public partial class ReminderTaskControl : UserControl
    {
        public ReminderTaskControl()
        {
            InitializeComponent();
            populateTaskReminderListView();
        }

        private void populateTaskReminderListView()
        {
            this.taskReminderListBox.ItemsSource = App.MyConfig.OurProject.Tasks;
        }

        private void TaskFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            String filter = ((ComboBoxItem)this.TaskFilterComboBox.SelectedItem).Tag as String;
            DateTime today = DateTime.Today;
            switch (filter) 
            {
                case "Late":
                    this.taskReminderListBox.Items.Filter =
                        (Predicate<object>)delegate(object item)
                        {
                            ProjectTask taskItem = item as ProjectTask;
                            int days = ((TimeSpan)(taskItem.Finish - today)).Days;
                            if (!taskItem.IsCompleted && days <= -1)
                                return true;
                            else return false;
                        };
                    break;
                case "Soon":
                    this.taskReminderListBox.Items.Filter =
                        (Predicate<object>)delegate(object item)
                        {
                            ProjectTask taskItem = item as ProjectTask;
                            int days = ((TimeSpan)(taskItem.Finish - today)).Days;
                            if (!taskItem.IsCompleted && days >= 0 && days <= 7)
                                return true;
                            else return false;
                        };
                    break;
                case "Later":
                    this.taskReminderListBox.Items.Filter =
                        (Predicate<object>)delegate(object item)
                        {
                            ProjectTask taskItem = item as ProjectTask;
                            int days = ((TimeSpan)(taskItem.Finish - today)).Days;
                            if (!taskItem.IsCompleted && days > 7 )
                                return true;
                            else return false;
                        };
                    break;
                case "Completed":
                    this.taskReminderListBox.Items.Filter =
                        (Predicate<object>)delegate(object item)
                        {
                            ProjectTask taskItem = item as ProjectTask;
                            if (taskItem.IsCompleted && !taskItem.IsMilestone)
                                return true;
                            else return false;
                        };
                    break;
                case "All":
                    this.taskReminderListBox.Items.Filter = null;
                    break;
            }
        }

        private void taskReminderListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.taskReminderListBox.SelectedIndex != -1)
            {
                int index = Convert.ToInt32(((ProjectTask)this.taskReminderListBox.SelectedItem).IndexString) - 1;
                if (index != -1)
                {
                    ProjectTask task = App.MyConfig.OurProject.Tasks[index];
                    this.taskNameLabel.Content = task.Content;
                    this.memberLabel.Content = task.AssignmentsString;
                    this.descriptionTextBlock.Text = task.Description;
                    this.startDateLabel.Content = task.Start.ToShortDateString();
                    this.endDateLabel.Content = task.Finish.ToShortDateString();
                }
            }
        }
    }
}
